import { Injectable } from '@angular/core';
import * as util from "app/util";
import feathers from 'feathers/client';
import socketio from 'feathers-socketio/client';
import io from 'socket.io-client/dist/socket.io';
import * as hooks from 'feathers-hooks';
import * as errors from 'feathers-errors';
import * as auth from 'feathers-authentication-client';

@Injectable()
export class FeathersService {

  public client: any
  public socket: any

  constructor() {
    this.socket = io(util.getConfig().server, {
      transports: ['websocket']
    });
    // this.client = feathers()
    //   .configure(socketio(this.socket))

    let server = "http://localhost:3031"
    let baseUrl = `${server}/authentication`
    let socket = io(server, {transports: ['websocket']});
    this.client = feathers()
      .configure(socketio(socket))
      .configure(hooks())
      .configure(auth())

    this.socket.on('news', (data) => {
        console.log('news:' + JSON.stringify(data));
      });

    let w: any = window
    w.feathersClient = this.client
    w.socketClient = this.socket
  }

}
